package cz.cvut.kadledav.paa.knapsack.tasks;

import cz.cvut.kadledav.paa.knapsack.bo.Item;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * User: David Kadlecek
 * Date: 8.10.13
 */
public abstract class AApproxOrderBasedTask extends ATask {

    protected Item[] items;
    protected Set<Integer> itemIds;

    @Override
    public void setItems(List<Item> items) {
        this.items = items.toArray(new Item[0]);
        itemIds = new HashSet<>(items.size());
    }

    @Override
    public void compute() {
        orderItems();
        int i = 0;
        int weight = 0;
        for (; i < items.length; i++) {
            int newWeight = weight + items[i].getWeight();
            if(newWeight>maxAllowedWeight){
                  continue;
            }
            maxPrice += items[i].getPrice();
            weight += items[i].getWeight();
            itemIds.add(items[i].getId());
        }
    }

    protected abstract void orderItems();

    @Override
    public Set<Integer> getComputedItemsIds() {
        return itemIds;
    }
}
